<?php

namespace App\Admin\Controllers;

use App\Models\SysDistrict;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class SysDistrictController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.sys_district'))
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.sys_district'))
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.sys_district'))
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.sys_district'))
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SysDistrict);
        $grid->model()->orderBy('city_id', 'asc')->orderBy('id', 'asc');
        $grid->id('Id');
        $grid->name(trans('admin.name'));
        $grid->city_name(trans('admin.city'))->display(function () {
            return $this->city->name;
        });
        $grid->province_name(trans('admin.province'))->display(function () {
            return $this->city->province->name;
        });
        $grid->post_code(trans('admin.post_code'));
        $grid->filter(function ($filter) {
            $filter->column(1 / 3, function ($filter) {
                $filter->like('name', trans('admin.name'));
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SysDistrict::findOrFail($id));
        $show->id('Id');
        $show->province_name(trans('admin.province'))->as(function () {
            return $this->city->province->name;
        });
        $show->city_name(trans('admin.city'))->as(function () {
            return $this->city->name;
        });
        $show->name(trans('admin.name'));
        $show->post_code(trans('admin.post_code'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SysDistrict);
        $form->select('province_id', trans('admin.province'))->options(route('admin.api.province'))
            ->load('city_id', route('admin.api.city'))
            ->rules(['required'])->setLabelClass(['required']);
        $form->select('city_id', trans('admin.city'))
            ->rules(['required'])
            ->setLabelClass(['required']);
        $form->text('name', trans('admin.name'))->rules(['required', 'unique:sys_district,name'])->setLabelClass(['required']);
        $form->text('post_code', trans('admin.post_code'))->rules(['required', 'unique:sys_district,post_code', 'integer'])->setLabelClass(['required']);
        $form->ignore(['province_id']);

        return $form;
    }
}
